﻿namespace = cid

#######################################
# El Cid story cycle
# By Veronica Pazos
#######################################

#######################################
###
###	ONGOING STORY CYCLE EVENTS
###
### 0001-0099 	Story Entry Points
###	0100-0199	Part I: Exile
### 0200-0299	Part II: Restoration
### 0400-0499	Conclusion
### 0500-0599 	Maintenance
###
#######################################

#######################################
###
###	ENTRY POINTS TO THE STORY
###	0001	Your liege despises you
###
#######################################

# By Order of the King
# You are exiled by your liege / Someone at court despises you / Historical flavour if Alfonso VI is your liege
# 0001

scripted_trigger cid_0001_hate_trigger = {
	OR = {
		has_relation_rival = root
		has_relation_nemesis = root
		opinion = {
			target = root
			value <= -50
		}
	}
}

scripted_effect create_el_cid_story = {
	create_story = {
		type = story_el_cid
		save_scope_as = new_story
	}
	random_owned_story = {
		limit = { story_type = story_el_cid }
		save_scope_as = story_el_cid
		story_owner = {
			set_variable = {
				name = cid_enemy
				value = flag:$ENEMY$
			}
			set_variable = {
				name = cid_liege
				value = scope:liege
			}
			set_variable = {
				name = cid_loyalty_counter
				value = 0
			}
			set_variable = ongoing_cid_story_cycle
		}
	}
}

cid.0001 = {
	type = character_event
	window = fullscreen_event
	title = cid.0001.t
	desc = {
		desc = cid.0001.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					var:cid_enemy = flag:alfonso
				}
				desc = cid.0001.desc.alfonso
			}
			triggered_desc = {
				trigger = {
					var:cid_enemy = flag:garcia
				}
				desc = cid.0001.desc.garcia_ordonez
			}
			triggered_desc = {
				trigger = {
					var:cid_enemy = flag:liege
				}
				desc = cid.0001.desc.liege_hate
			}
			desc = cid.0001.desc.courtier
		}
		desc = cid.0001.desc.outro
	}
	theme = laamp
	left_portrait = {
		character = root
		animation = wailing
	}
	right_portrait = {
		character = scope:liege
		animation = personality_callous
	}
	lower_right_portrait = {
		trigger = { exists = scope:bad_courtier }
		character = scope:bad_courtier
	}
	override_background = { reference = ep3_fullscreen_adventurer_negative }

	immediate = {		
		location.county.holder.top_liege = { save_scope_as = liege }
		if = { #Historical conditions first
			limit = { scope:liege = character:108500 }
			hidden_effect = { # We show this in the "after"
				create_el_cid_story = { ENEMY = alfonso }
			}
		}
		else_if = { #If your liege hates you that takes preferent
			limit = {
				scope:liege = { cid_0001_hate_trigger = yes }
			}
			hidden_effect = {
				create_el_cid_story = { ENEMY = liege }
			}
		}
		else_if = { #If Garcia Ordoñez is there then it's him even if he "likes" you
			limit = {
				character:106050 ?= {
					is_alive = yes
					top_liege = scope:liege
				}
			}
			character:106050 = {
				save_scope_as = bad_courtier
			}
			hidden_effect = {
				create_el_cid_story = { ENEMY = garcia }
			}
		}
		else_if = { #If a random courtier hates you
			limit = {
				scope:liege = {
					any_courtier = {
						location = scope:liege.location
						cid_0001_hate_trigger = yes
					}
				}
			}
			scope:liege = {
				random_courtier = {
					limit = {
						cid_0001_hate_trigger = yes
					}
					weight = {
						#Choose someone that really hates you first
						modifier = {
							add = 15
							has_relation_nemesis = root
						}
						modifier = {
							add = 10
							has_relation_rival = root
						}
						modifier = {
							add = 5
							opinion = {
								target = root
								value <= -90
							}
						}
						#Try to make it someone relevant
						modifier = {
							add = 5
							is_councillor_of = scope:liege
						}
						modifier = {
							add = 5
							OR = {
								is_close_family_of = scope:liege
								is_consort_of = scope:liege
							}
						}
						modifier = {
							add = 5
							is_powerful_vassal_of = scope:liege
						}
						modifier = {
							add = 2
							has_trait = callous
						}
						modifier = {
							add = 2
							has_trait = disloyal
						}
						modifier = {
							add = 2
							has_trait = ambitious
						}
					}
					save_scope_as = bad_courtier
				}
			}
			hidden_effect = {
				create_el_cid_story = { ENEMY = courtier }
			}
		}
		else = { #We make someone hate you
			scope:liege = {
				random_courtier = {
					limit = {
						is_available_healthy_ai_adult = yes
					}
					weight = {
						#Try to make it someone relevant
						modifier = {
							add = 5
							is_councillor_of = scope:liege
						}
						modifier = {
							add = 5
							OR = {
								is_close_family_of = scope:liege
								is_consort_of = scope:liege
							}
						}
						modifier = {
							add = 5
							is_powerful_vassal_of = scope:liege
						}
						modifier = {
							add = 2
							has_trait = callous
						}
						modifier = {
							add = 2
							has_trait = disloyal
						}
						modifier = {
							add = 2
							has_trait = ambitious
						}
					}
					save_scope_as = bad_courtier
				}
			}
			hidden_effect = {
				scope:bad_courtier = {
					add_opinion = {
						target = root
						opinion = -75
						modifier = hate_opinion
					}
				}
				create_el_cid_story = { ENEMY = courtier }
			}
		}
	}

	# I will always be loyal to you
	option = {
		name = cid.0001.a
		flavor = cid.0001.a.flavor
		custom_tooltip = story_el_cid_tt
		custom_tooltip = cid_loyalty_counter_positive
		change_variable = {
			name = cid_loyalty_counter
			add = 1
		}
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# I shall become my own liege
	option = {
		name = cid.0001.b
		flavor = cid.0001.b.flavor
		custom_tooltip = story_el_cid_tt
		custom_tooltip = cid_loyalty_counter_negative
		change_variable = {
			name = cid_loyalty_counter
			add = -1
		}
		ai_chance = {
			base = 0 # Historically determined
		}
	}
	
	# Opt out
	option = {
		name = ep3_story_cycle_admin_eunuch.2043.b
		scope:story_el_cid = { end_story = yes }
		ai_chance = {
			base = 0 # Historically determined
		}
	}
	
	after = { # To make it look nicer in the tooltip
		scope:liege ?= {
			if = {
				limit = { has_relation_friend = root }
				remove_relation_friend = root
			}
		}
		create_title_and_vassal_change = {
			type = independency
			add_claim_on_loss = yes
			save_scope_as = change
		}
		becomes_independent = { change = scope:change }
		resolve_title_and_vassal_change = scope:change
	}
}

#######################################
###
###	PART I: EXILE
###	1000	No one opens the door to you
###	1010	You have no money to pay your men
### 1020	You have to leave your spouse/children behind
### 1030	You have some money - send a gift?
### 1040	You pray for your liege
###
#######################################

# Closed Doors
# No one opens the door for you
# 1000

cid.1000 = {
	type = character_event
	title = cid.1000.t
	desc = cid.1000.desc
	theme = laamp
	left_portrait = {
		character = root
		animation = worry
	}
	right_portrait = {
		character = scope:peasant
		animation = beg
	}
	lower_right_portrait = {
		trigger = { exists = scope:liege }
		character = scope:liege
	}
	override_background = { reference = alley_night }
	override_effect_2d = {
		reference = rain	
	}

	trigger = {
		is_landless_adventurer = yes
		NOT = { has_variable = had_cid_1000 }
	}

	immediate = {
		set_variable = had_cid_1000
		var:cid_liege = { save_scope_as = liege }
		if = {
			limit = {
				any_pool_character = {
					province = root.location
					is_adult = yes
				}
			}
			random_pool_character = {
				province = root.location
				limit = {
					is_adult = yes
				}
				save_scope_as = peasant
			}
		}
		else = {
			hidden_effect = {
				create_basic_peasant_effect = yes
			}
		}
	}

	# Let's just leave
	option = {
		name = cid.1000.a
		flavor = cid.1000.a.flavor
		custom_tooltip = cid_loyalty_counter_positive
		domicile = {
			change_provisions = medium_provisions_loss
		}
		change_variable = {
			name = cid_loyalty_counter
			add = 1
		}
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# Open the door
	option = {
		name = cid.1000.b
		flavor = cid.1000.b.flavor
		custom_tooltip = cid_loyalty_counter_negative
		add_character_modifier = {
			modifier = cid_opened_doors_modifier
			years = 10
		}
		domicile = {
			change_provisions = minor_provisions_gain
		}
		change_variable = {
			name = cid_loyalty_counter
			add = -1
		}
		ai_chance = {
			base = 0 # Historically determined
		}
	}
}

# No Gold or Silver
# You cannot afford your men
# 1010

scripted_trigger cid_1010_courtier_trigger = {
	is_adult = yes
	NOR = {
		is_close_family_of = root
		is_spouse_of = root
	}
}

cid.1010 = {
	type = character_event
	title = cid.1010.t
	desc = cid.1010.desc
	theme = laamp
	left_portrait = {
		character = root
		animation = wedding_happy_cry
	}
	right_portrait = {
		character = scope:friend
		animation = lantern
		camera = camera_event_right_pointing_right
	}
	lower_left_portrait = scope:courtier_1
	lower_center_portrait = {
		trigger = { exists = scope:courtier_2 }
		character = scope:courtier_2
	}
	lower_right_portrait = {
		trigger = { exists = scope:courtier_3 }
		character = scope:courtier_3
	}
	override_background = { reference = ep2_hunt_foggy_forest }
	override_effect_2d = {
		reference = fog	
	}

	trigger = {
		is_landless_adventurer = yes
		any_courtier = {
			cid_1010_courtier_trigger = yes
			count >= 2
		}
		NOT = { has_variable = had_cid_1010 }
	}

	immediate = {
		set_variable = had_cid_1010
		var:cid_liege = { save_scope_as = liege }
		random_courtier = {
			limit = { cid_1010_courtier_trigger = yes }
			weight = {
				modifier = {
					add = 15
					has_relation_best_friend = root
				}
				modifier = {
					add = 10
					has_relation_friend = root
				}
				modifier = {
					add = 5
					opinion = {
						target = root
						value >= 50
					}
				}
			}
			save_scope_as = friend
		}
		random_courtier = {
			limit = {
				cid_1010_courtier_trigger = yes
				NOT = { this = scope:friend }
			}
			weight = {
				modifier = {
					add = 5
					has_any_court_position = yes
				}
			}
			save_scope_as = courtier_1
		}
		if = {
			limit = {
				any_courtier = {
					cid_1010_courtier_trigger = yes
					NOT = { this = scope:friend }
					NOT = { this = scope:courtier_1 }
				}
			}
			random_courtier = {
				limit = {
					cid_1010_courtier_trigger = yes
					NOT = { this = scope:friend }
					NOT = { this = scope:courtier_1 }
				}
				weight = {
					modifier = {
						add = 5
						has_any_court_position = yes
					}
				}
				save_scope_as = courtier_2
			}
		}
		if = {
			limit = {
				any_courtier = {
					cid_1010_courtier_trigger = yes
					NOT = { this = scope:friend }
					NOT = { this = scope:courtier_1 }
					NOT = { this = scope:courtier_2 }
				}
			}
			random_courtier = {
				limit = {
					cid_1010_courtier_trigger = yes
					NOT = { this = scope:friend }
					NOT = { this = scope:courtier_1 }
					NOT = { this = scope:courtier_2 }
				}
				weight = {
					modifier = {
						add = 5
						has_any_court_position = yes
					}
				}
				save_scope_as = courtier_3
			}
		}
		if = {
			limit = { employs_court_position = camp_cook_camp_officer }
			random_court_position_holder = {
				type = camp_cook_camp_officer
				save_scope_as = cook
			}
		}
	}
	
	# Special option if you have a Camp Cook
	option = {
		name = cid.1010.a
		add_internal_flag = special
		trigger = { employs_court_position = camp_cook_camp_officer }
		domicile = {
			change_provisions = minor_provisions_loss
		}
		
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# Thanks, amigo
	option = {
		name = cid.1010.b
		flavor = cid.1010.b.flavor
		custom_tooltip = cid_loyalty_counter_negative
		if = {
			limit = {
				NOT = { has_relation_friend = scope:friend }
			}
			set_relation_friend = {
				target = scope:friend
				reason = friend_cid_lent_gold
			}
		}
		else = {
			scope:friend = {
				add_opinion = {
					target = root
					modifier = strengthened_friendship_opinion
				}
			}
		}
		add_gold = medium_gold_value
		change_variable = {
			name = cid_loyalty_counter
			add = -1
		}		
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# Dont anger the king further
	option = {
		name = cid.1010.c
		custom_tooltip = cid_loyalty_counter_very_positive
		remove_courtier_or_guest = scope:courtier_1
		scope:courtier_1 = {
			set_variable = {
				name = cid_1010_no_invite_var
				value = root
			}
		}
		if = {
			limit = { exists = scope:courtier_2 }
			remove_courtier_or_guest = scope:courtier_2
			scope:courtier_2 = {
				set_variable = {
					name = cid_1010_no_invite_var
					value = root
				}
			}
		}
		if = {
			limit = { exists = scope:courtier_3 }
			remove_courtier_or_guest = scope:courtier_3
			scope:courtier_3 = {
				set_variable = {
					name = cid_1010_no_invite_var
					value = root
				}
			}
		}
		change_variable = {
			name = cid_loyalty_counter
			add = 2
		}
		ai_chance = {
			base = 0 # Historically determined
		}
	}
}

# Farewell, My Dear
# You leave your spouse/children behind
# 1020

cid.1020 = {
	type = character_event
	title = cid.1020.t
	desc = {
		desc = cid.1020.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:child_1
				}
				desc = cid.0001.desc.children
			}
			desc = cid.0001.desc.no_children
		}
	}
	theme = laamp
	left_portrait = {
		character = root
		animation = grief
	}
	right_portrait = {
		character = scope:spouse
		animation = wailing
	}
	lower_left_portrait = {
		trigger = { exists = scope:child_1 }
		character = scope:child_1
	}
	lower_center_portrait = {
		trigger = { exists = scope:child_2 }
		character = scope:child_2
	}
	lower_right_portrait = {
		trigger = { exists = scope:priest }
		character = scope:priest
	}
	override_background = { reference = temple }

	trigger = {
		any_courtier = {
			is_spouse_of = root 
		}
		NOT = { has_variable = had_cid_1020 }
	}

	immediate = {
		set_variable = had_cid_1020
		var:cid_liege = { save_scope_as = liege }
		random_courtier = {
			limit = { is_spouse_of = root }
			weight = {
				modifier = {
					add = 15
					has_relation_soulmate = root
				}
				modifier = {
					add = 5
					has_relation_lover = root
				}
				modifier = {
					add = 5
					this = root.primary_spouse
				}
			}
			save_scope_as = spouse
		}
		if = {
			limit = {
				any_courtier = { is_child_of = root }
			}
			random_courtier = {
				limit = { is_child_of = root }
				weight = {
					modifier = {
						add = 5
						is_primary_heir_of = root
					}
				}
				save_scope_as = child_1
			}
		}
		if = {
			limit = {
				any_courtier = {
					is_child_of = root
					NOT = { this = scope:child_1 }
				}
			}
			random_courtier = {
				limit = {
					is_child_of = root
					NOT = { this = scope:child_1 }
				}
				save_scope_as = child_2
			}
		}
		if = {
			limit = {
				employs_court_position = camp_priest_camp_officer
			}
			random_court_position_holder = {
				type = camp_priest_camp_officer
				save_scope_as = my_priest
			}
		}
	}
	
	# Special option if you have a Priest that can intercede
	option = {
		name = cid.1020.a
		add_internal_flag = special
		trigger = {
			employs_court_position = camp_priest_camp_officer
		}
		remove_short_term_gold = minor_gold_value
		add_piety = medium_piety_value
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# I shall miss you dearly
	option = {
		name = cid.1020.b
		flavor = cid.1020.b.flavor
		custom_tooltip = cid_loyalty_counter_positive
		
		location.county.holder = {
			add_courtier = scope:spouse
		}
		if = {
			limit = { exists = scope:child_1 }
			location.county.holder = { add_courtier = scope:child_1 }
		}
		if = {
			limit = { exists = scope:child_2 }
			location.county.holder = { add_courtier = scope:child_2 }
		}
		
		change_variable = {
			name = cid_loyalty_counter
			add = 1
		}
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# I cannot do this
	option = {
		name = cid.1020.c
		flavor = cid.1020.c.flavor
		custom_tooltip = cid_loyalty_counter_very_negative
		if = {
			limit = {
				NOT = {
					can_set_relation_lover_trigger = { CHARACTER = scope:spouse }
				}
			}
			set_relation_lover = {
				target = scope:spouse
				reason = cid_lover_refused_to_left_behind
			}
		}
		add_character_modifier = {
			modifier = cid_family_good_modifier
			years = 10
		}
		change_variable = {
			name = cid_loyalty_counter
			add = -2
		}
		ai_chance = {
			base = 0 # Historically determined
		}
	}
}

# For the Love of the King
# You have some money, send gift?
# 1030

cid.1030 = {
	type = character_event
	title = cid.1030.t
	desc = cid.1030.desc
	theme = laamp
	left_portrait = {
		character = root
		animation = thinking
	}
	lower_right_portrait = scope:liege
	artifact = {
		trigger = { exists = scope:artifact }
		target = scope:artifact
		position = lower_left_portrait
	}

	trigger = {
		NOT = { has_variable = had_cid_1030 }
		gold >= 50
	}

	immediate = {
		set_variable = had_cid_1030
		var:cid_liege = { save_scope_as = liege }
		if = {
			limit = {
				any_character_artifact = {
					OR = { 
						rarity = illustrious
						rarity = famed 
					}
				}
			}
			random_character_artifact = {
				limit = {
					OR = { 
						rarity = masterwork
						rarity = illustrious
						rarity = famed 
					}
				}
				weight = {
					modifier = {
						add = 10
						is_equipped = no
					}
					modifier = {
						add = 2
						rarity = famed
					}
					modifier = {
						add = 2
						rarity = illustrious
					}
				}
				save_scope_as = artifact
			}
		}
	}

	# If you have an artifact send that instead?
	option = {
		name = cid.1030.a
		trigger = { exists = scope:artifact }
		add_internal_flag = special
		custom_tooltip = cid_loyalty_counter_positive
		scope:artifact = { set_owner = scope:liege }
		change_variable = {
			name = cid_loyalty_counter
			add = 1
		}
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# Send money
	option = {
		name = cid.1030.b
		custom_tooltip = cid_loyalty_counter_positive
		pay_short_term_gold = {
			target = scope:liege
			gold = medium_gold_value
		}
		change_variable = {
			name = cid_loyalty_counter
			add = 1
		}
		ai_chance = {
			base = 1000 # Historically determined
		}
	}
	
	# No, thanks
	option = {
		name = cid.1030.c
		custom_tooltip = cid_loyalty_counter_negative
		
		change_variable = {
			name = cid_loyalty_counter
			add = -1
		}
		ai_chance = {
			base = 0
		}
	}
}

# May the Lord Have Mercy
# Publicly pray for your liege
# 1040

cid.1040 = {
	type = character_event
	title = cid.1040.t
	desc = cid.1040.desc
	theme = laamp
	left_portrait = {
		character = root
		animation = prayer
	}
	lower_right_portrait = scope:liege
	override_background = { reference = temple }

	trigger = {
		NOT = { has_variable = had_cid_1040 }
	}

	immediate = {
		set_variable = had_cid_1040
		var:cid_liege = { save_scope_as = liege }
	}

	# You do it in your own camp, dayum
	option = {
		name = cid.1040.a
		custom_tooltip = cid_1040_a_tt
		trigger = {
			domicile = { has_domicile_building = baggage_train_shrine }
		}
		add_internal_flag = special
		
		custom_tooltip = cid_loyalty_counter_very_positive
		
		change_variable = {
			name = cid_loyalty_counter
			add = 2
		}
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# Pray for them
	option = {
		name = cid.1040.b
		custom_tooltip = cid_loyalty_counter_positive
		change_variable = {
			name = cid_loyalty_counter
			add = 1
		}
		ai_chance = {
			base = 1000 # Historically determined
		}
	}
	
	# Pray for yourself
	option = {
		name = cid.1040.c
		custom_tooltip = cid_loyalty_counter_negative
		add_character_modifier = {
			modifier = cid_own_prayer
			years = 15
		}
		change_variable = {
			name = cid_loyalty_counter
			add = -1
		}
		ai_chance = {
			base = 0
		}
	}
}

#######################################
###
###	PART II: RESTORATION
###	2000	You win Tizona
### 2001	You win Colada
###	2010	Your liege offers you a favourable marriage
### 2020	You become landed again
###
#######################################

# From the Elbow Down
# You win Tizona
# 2000

cid.2000 = {
	type = character_event
	title = cid.2000.t
	desc = {
		desc = cid.2000.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:loser }
				desc = cid.2000.desc.loser
			}
			desc = cid.2000.desc.fallback
		}
		desc = cid.2000.desc.outro
	}
	theme = laamp
	left_portrait = {
		character = root
		animation = jockey_victory
		camera = camera_event_horse_left
	}
	right_portrait = {
		trigger = { exists = scope:loser }
		character = scope:loser
		animation = severelywounded
	}
	artifact = {
		target = scope:newly_created_artifact
		position = lower_center_portrait
	}
	lower_right_portrait = scope:liege
	override_background = { reference = ce1_legendary_battlefield }
	override_effect_2d = {
		reference = legend_glow	
	}

	trigger = {
		NOT = { has_variable = had_cid_2000 }
		has_variable = ongoing_cid_story_cycle
	}

	immediate = {
		set_variable = had_cid_2000
		var:cid_liege = { save_scope_as = liege }
		# Create the artifact
		if = { # Court Artifact
			limit = { has_royal_court = yes }
			create_artifact = {
				name = artifact_wall_cid_sword_name
				description = artifact_wall_el_cids_sword
				type = pedestal
				visuals = wall_cid_sword
				wealth = 90
				quality = 90
				template = tizona_template
				history = {
					type = created_before_history
				}
				modifier = tizona_modifier
				save_scope_as = newly_created_artifact
				decaying = no
			}
		}
		else = {
			hidden_effect_new_object = {
				create_artifact = {
					name = artifact_wall_cid_sword_name
					description = artifact_wall_el_cids_sword
					type = sword
					visuals = sword
					modifier = tizona_no_court_modifier
					wealth = 90
					quality = 90
					save_scope_as = newly_created_artifact
				}
			}
		}
	}

	# I win!
	option = {
		name = cid.2000.a
		flavor = cid.2000.a.flavor
		custom_tooltip = cid_loyalty_counter_negative
		custom_tooltip = cid.2001.a.tt
		change_variable = {
			name = cid_loyalty_counter
			add = -1
		}
		add_gold = medium_gold_value
		
		#If you have CE1 we give you a legend seed
		if = {
			limit = {
				has_dlc_feature = legends
				NOT = { has_variable = had_cid_2001 }
				exists = scope:loser
				NOT = { has_game_rule = historical_legends_only }
			}
			create_legend_seed = {
				type = heroic
				quality = famed
				chronicle = legendary_battle
				properties = {
					winner = root
					loser = scope:loser
					location = root.location
				}
			}
		}
		
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# Send the spoils to the king
	option = {
		name = cid.2000.b
		flavor = cid.2000.b.flavor
		custom_tooltip = cid_loyalty_counter_positive
		change_variable = {
			name = cid_loyalty_counter
			add = 1
		}
		scope:liege = {
			add_gold = medium_gold_value
			scope:newly_created_artifact = { set_owner = scope:liege }
		}
		
		ai_chance = {
			base = 0 # Historically determined
		}
	}
}

# From a Brave Master
# You win Colada
# 2001

cid.2001 = {
	type = character_event
	title = cid.2001.t
	desc = {
		desc = cid.2001.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:loser }
				desc = cid.2001.desc.loser
			}
			desc = cid.2001.desc.fallback
		}
		desc = cid.2001.desc.outro
		first_valid = {
			triggered_desc = {
				trigger = { has_variable = had_cid_2000 }
				desc = cid.2001.desc.outro.tizona
			}
		}
	}
	theme = laamp
	left_portrait = {
		character = root
		animation = sword_coup_degrace
	}
	right_portrait = {
		trigger = { exists = scope:loser }
		character = scope:loser
		animation = severelywounded
	}
	artifact = {
		target = scope:newly_created_artifact
		position = lower_center_portrait
	}
	lower_right_portrait = scope:liege
	override_background = { reference = ce1_legendary_battlefield }
	override_effect_2d = {
		reference = rain	
	}

	trigger = {
		NOT = { has_variable = had_cid_2001 }
		has_variable = ongoing_cid_story_cycle
	}

	immediate = {
		set_variable = had_cid_2001
		var:cid_liege = { save_scope_as = liege }
		# Create the artifact
		if = { # Court Artifact
			limit = { has_royal_court = yes }
			hidden_effect_new_object = {
				create_artifact = {
					name = artifact_colada_name
					description = artifact_colada_description
					type = pedestal
					visuals = wall_cid_sword
					wealth = 75
					quality = 75
					template = general_unique_template
					history = {
						type = created_before_history
					}
					modifier = colada_modifier
					save_scope_as = newly_created_artifact
					decaying = no
				}
			}
		}
		else = {
			hidden_effect_new_object = {
				create_artifact = {
					name = artifact_colada_name
					description = artifact_colada_description
					type = sword
					visuals = sword
					modifier = colada_non_court_modifier
					wealth = 75
					quality = 75
					save_scope_as = newly_created_artifact
				}
			}
		}
	}

	# I win!
	option = {
		name = cid.2001.a
		flavor = cid.2001.a.flavor
		custom_tooltip = cid_loyalty_counter_negative
		custom_tooltip = cid.2001.a.tt
		change_variable = {
			name = cid_loyalty_counter
			add = -1
		}
		add_gold = medium_gold_value
		
		#If you have CE1 we give you a legend seed
		if = {
			limit = {
				has_dlc_feature = legends
				NOT = { has_variable = had_cid_2000 }
				NOT = { has_game_rule = historical_legends_only }
			}
			create_legend_seed = {
				type = heroic
				quality = famed
				chronicle = legendary_battle
				properties = {
					winner = root
					loser = scope:loser
					location = root.location
				}
			}
		}
		
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# Send the spoils to the king
	option = {
		name = cid.2001.b
		custom_tooltip = cid_loyalty_counter_positive
		change_variable = {
			name = cid_loyalty_counter
			add = 1
		}
		scope:liege = {
			add_gold = medium_gold_value
			scope:newly_created_artifact = { set_owner = scope:liege }
		}
		
		ai_chance = {
			base = 0 # Historically determined
		}
	}
}

# A Favorable Offer
# Your liege offers you a favourable marriage
# 2010

scripted_trigger cid_2010_possible_child_trigger = {
	is_child_of = root
	trigger_if = {
		limit = {
			NOR = {
				hp_accepts_polygamy = yes
				allowed_more_spouses = yes
			}
		}
		is_married = no
		is_betrothed = no
	}
}

scripted_trigger cid_2010_herald_trigger = {
	is_capable_adult = yes
	NOR = {
		is_close_family_of = scope:liege
		is_spouse_of = scope:liege
	}
	NOT = { has_council_position = councillor_court_chaplain }
}

scripted_effect cid_2010_create_proposed_spouse_effect = {
	create_character = {
		template = knight
		dynasty = none
		location = scope:liege.location
		culture = scope:liege.location.culture
		faith = scope:liege.location.faith
		gender_female_chance = $CHANCE$
		save_scope_as = proposed_spouse
	}
}

cid.2010 = {
	type = character_event
	title = cid.2010.t
	desc = cid.2010.desc
	theme = laamp
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:herald
		animation = chancellor
	}
	lower_left_portrait = {
		character = scope:child
	}
	lower_right_portrait = {
		character = scope:proposed_spouse
	}

	trigger = {
		any_courtier = {
			cid_2010_possible_child_trigger = yes
		}
		var:cid_loyalty_counter >= 2
		NOT = { has_variable = had_cid_2010 }
	}

	immediate = {
		set_variable = had_cid_2010
		var:cid_liege = { save_scope_as = liege }
		if = {
			limit = {
				scope:liege = {
					any_courtier = { cid_2010_herald_trigger = yes }
				}
			}
			scope:liege = {
				random_courtier = {
					limit = {
						cid_2010_herald_trigger = yes
					}
					save_scope_as = herald
				}
			}
		}
		else = {
			create_character = {
				template = herald_character
				dynasty = none
				location = scope:liege.location
				culture = scope:liege.location.culture
				faith = scope:liege.location.faith
				gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
				save_scope_as = herald
			}
			scope:liege = { add_courtier = scope:herald }
		}
		random_courtier = {
			limit = {
				cid_2010_possible_child_trigger = yes
			}
			weight = {
				modifier = {
					add = 5
					is_primary_heir_of = root
				}
				modifier = {
					add = 2
					age >= 16
				}
			}
			save_scope_as = child
		}
		if = {
			limit = {
				scope:liege = {
					any_close_or_extended_family_member = {
						can_marry_character_trigger = {
							CHARACTER = scope:child
						}
					}
				}
			}
			scope:liege = {
				random_close_or_extended_family_member = {
					limit = {
						can_marry_character_trigger = {
							CHARACTER = scope:child
						}
					}
					weight = { # Good candidates 
						modifier = {
							add = 5
							is_close_family_of = scope:liege
						}
						modifier = { # Age appropriate
							add = 5
							age <= scope:child.age_plus_5
							age >= scope:child.age_minus_5
						}
					}
					save_scope_as = proposed_spouse
				}
			}
		}
		else_if = {
			limit = {
				scope:liege = {
					any_courtier = {
						can_marry_character_trigger = {
							CHARACTER = scope:child
						}
					}
				}
			}
			scope:liege = {
				random_courtier = {
					limit = {
						can_marry_character_trigger = {
							CHARACTER = scope:child
						}
					}
					weight = { # Age appropriate
						modifier = {
							add = 5
							age <= scope:child.age_plus_5
							age >= scope:child.age_minus_5
						}
						modifier = { # Knights are just cool
							add = 2
							is_knight_of = scope:liege
						}
					}
					save_scope_as = proposed_spouse
				}
			}
		}
		else = {
			if = { # We make them a woman if your child is a man/woman that can marry a woman
				limit = {
					scope:child = {
						OR = {
							AND = {
								is_female = yes
								OR = {
									has_sexuality = homosexual
									has_sexuality = bisexual
								}
								allowed_to_marry_same_sex_trigger = yes
							}
							AND = {
								is_male = yes
								OR = {
									has_sexuality = heterosexual
									has_sexuality = bisexual
								}
							}
						}
					}
				}
				cid_2010_create_proposed_spouse_effect = { CHANCE = 100 }
			}
			else = {
				cid_2010_create_proposed_spouse_effect = { CHANCE = 0 }
			}
			scope:proposed_spouse = { add_character_flag = wear_armor }
			scope:liege = { add_courtier = scope:proposed_spouse }
		}
	}

	# Sure
	option = {
		name = cid.2010.a
		flavor = cid.2010.a.flavor
		custom_tooltip = cid_loyalty_counter_very_positive
		scope:child = { create_betrothal = scope:proposed_spouse }
		change_variable = {
			name = cid_loyalty_counter
			add = 2
		}
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# No way
	option = {
		name = cid.2010.b
		custom_tooltip = cid_loyalty_counter_very_negative
		add_character_modifier = {
			modifier = cid_refused_betrothal
			years = 10
		}
		change_variable = {
			name = cid_loyalty_counter
			add = -2
		}
		ai_chance = {
			base = 0 # Historically determined
		}
	}
}

# LOYAL ENDING
# An Honorable Man
# You become landed again
# 2020

scripted_effect cid_2020_loyal_effect = {
	if = {
		limit = { NOT = { has_trait = loyal } }
		add_trait_force_tooltip = loyal
	}
}

cid.2020 = {
	type = character_event
	title = cid.2020.t
	desc = cid.2020.desc
	theme = laamp
	left_portrait = {
		character = root
		animation = wedding_happy_cry
	}
	right_portrait = {
		character = scope:liege
		animation = ecstasy
	}

	trigger = {
		#triggered through the story cycle
		var:cid_liege.primary_title = {
			any_de_jure_county = {
				holder = root.var:cid_liege
				NOR = {
					root.var:cid_liege.capital_county ?= this
					root.var:cid_liege.primary_title ?= this
				}
			}
		}
		NOT = { has_variable = had_cid_2020 }
	}

	immediate = {
		if = {
			limit = {
				is_ai = no
			}
			add_achievement_global_variable_effect = {
				VARIABLE = finished_mio_cid_achievement
				VALUE = yes
			}
		}
		set_variable = had_cid_2020
		var:cid_liege = { save_scope_as = liege }
		scope:liege.primary_title = {
			every_de_jure_county = {
				limit = {
					holder = scope:liege
					NOR = {
						scope:liege.capital_county ?= this
						scope:liege.primary_title ?= this
					}
				}
				add_to_list = granted_title
			}
		}
	}

	# Wahoo!
	option = {
		name = cid.2020.a
		flavor = cid.2020.a.flavor
		cid_2020_loyal_effect = yes
		# Resolve title, liege, government changes
		ep3_become_landed_save_liege_effect = {
			TITLE_GIVER = root.var:cid_liege
			ALWAYS_INDEPENDENT = no
			TITLE_LIST = granted_title
		}
		ep3_become_landed_transfer_no_ennobled_modifier_effect = {
			TITLE_RECEIVER = root
			TITLE_LIST = granted_title
			TYPE = granted
			REASON = flag:negotiated
		}
		
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# I'm good
	option = {
		name = cid.2020.b
		flavor = cid.2020.b.flavor
		custom_tooltip = cid_2020_b_tt
		cid_2020_loyal_effect = yes
		add_prestige = major_prestige_gain
		domicile = {
			change_provisions = medium_provisions_gain
		}
		if = {
			limit = {
				can_add_hook = {
					target = scope:liege
					type = loyalty_hook
				}
			}
			add_hook = {
				target = scope:liege
				type = loyalty_hook
			}
		}
		
		ai_chance = {
			base = 0 # Historically determined
		}
	}
	
	# I'm good and also screw you
	option = {
		name = cid.2020.c
		custom_tooltip = cid_2020_b_tt		
		scope:liege = {
			add_opinion = {
				target = root
				opinion = -50
				modifier = disrespect_opinion
			}
		}
		
		ai_chance = {
			base = 0 # Historically determined
		}
	}
	
	after = {
		random_owned_story = {
			limit = {
				story_type = story_el_cid
			}
			end_story = yes
		}
	}
}

#######################################
###
###	CONCLUSION
###
### # Good Ending given out in 2020 #
###	4000	Neutral Ending
### 4010	Disloyal Ending
###
#######################################

# The End of the Song
# Neutral Ending
# 4000

cid.4000 = {
	type = letter_event
	opening = {
		desc = cid.4000.opening
	}
	desc = cid.4000.desc
	sender = scope:sender

	trigger = {
		NOT = { has_variable = had_cid_4000 }
	}

	immediate = {
		set_variable = had_cid_4000
		var:cid_liege = { save_scope_as = sender }
	}

	# Bleh
	option = {
		name = cid.4000.a
		if = { #You didn't manage to max out either way
			limit = {
				NOT = { has_trait = fickle }
			}
			add_trait_force_tooltip = fickle
		}
		ai_chance = {
			base = 100
		}
	}
	
	after = {
		random_owned_story = {
			limit = {
				story_type = story_el_cid
			}
			end_story = yes
		}
	}
}

# Mio Cid
# Disloyal Ending: You become landed again
# 4010

scripted_effect cid_4010_disloyal_effect = {
	if = {
		limit = { NOT = { has_trait = disloyal } }
		add_trait_force_tooltip = disloyal
	}
}

cid.4010 = {
	type = character_event
	title = cid.4010.t
	desc = cid.4010.desc
	theme = laamp
	left_portrait = {
		character = root
		animation = dismissal
	}
	right_portrait = {
		character = scope:herald
		animation = anger
	}
	lower_right_portrait = scope:liege

	trigger = {
		#triggered through the story cycle <= -5 loyalty
		var:cid_liege.primary_title = {
			any_de_jure_county = {
				holder = root.var:cid_liege
				NOR = {
					root.var:cid_liege.capital_county ?= this
					root.var:cid_liege.primary_title ?= this
				}
			}
		}
		NOT = { has_variable = had_cid_4010 }
	}

	immediate = {
		set_variable = had_cid_4010
		var:cid_liege = { save_scope_as = liege }
		if = {
			limit = {
				scope:liege = {
					any_courtier = { cid_2010_herald_trigger = yes }
				}
			}
			scope:liege = {
				random_courtier = {
					limit = {
						cid_2010_herald_trigger = yes
					}
					save_scope_as = herald
				}
			}
		}
		else = {
			create_character = {
				template = herald_character
				dynasty = none
				location = scope:liege.location
				culture = scope:liege.location.culture
				faith = scope:liege.location.faith
				gender_female_chance = root_faith_dominant_gender_adjusted_female_chance
				save_scope_as = herald
			}
			scope:liege = { add_courtier = scope:herald }
		}
	}

	# Wahoo! Get landed and independent
	option = {
		name = cid.4010.a
		flavor = cid.4010.a.flavor
		trigger = { has_variable = cid_title_to_give }
		cid_4010_disloyal_effect = yes
		var:cid_title_to_give = {
			every_de_jure_county = { add_to_list = seized_titles }
		}
		ep3_become_landed_save_liege_effect = {
			TITLE_GIVER = var:cid_title_to_give.holder
			ALWAYS_INDEPENDENT = yes
			TITLE_LIST = seized_titles
		}
		ep3_become_landed_transfer_no_ennobled_modifier_effect = {
			TITLE_RECEIVER = root
			TITLE_LIST = seized_titles
			TYPE = usurped
			REASON = flag:seize_realm
		}
		
		ai_chance = {
			base = 1000 # Historically determined
		}
	}

	# I'm good
	option = {
		name = cid.4010.b
		flavor = cid.4010.b.flavor
		custom_tooltip = cid_2020_b_tt
		cid_4010_disloyal_effect = yes
		add_prestige = medium_prestige_gain
		
		ai_chance = {
			base = 500 # Historically determined
		}
	}
	
	# Actually... the king is not that bad
	option = {
		name = cid.4010.c
		flavor = cid.4010.c.flavor
		custom_tooltip = cid_2020_b_tt
		domicile = { change_provisions = medium_provisions_gain }
		if = {
			limit = {
				scope:liege = {
					can_add_hook = {
						target = root
						type = favor_hook
					}
				}
			}
			scope:liege = {
				add_hook = {
					target = root
					type = favor_hook
				}
			}
		}
		progress_towards_friend_effect = {
			REASON = friend_cid_liege_repentance
			CHARACTER = scope:liege
			OPINION = 50
		}
		
		ai_chance = {
			base = 0 # Historically determined
		}
	}
	
	after = {
		random_owned_story = {
			limit = {
				story_type = story_el_cid
			}
			end_story = yes
		}
	}
}

#######################################
###
###	MAINTENANCE
###	5000	Liege dies (letter event)
###
#######################################

# Your liege died
# 5000

cid.5000 = {
	type = letter_event
	opening = {
		desc = cid.5000.opening
	}
	desc = cid.5000.desc
	sender = scope:sender
	
	immediate = {
		location.county.holder.top_liege = { save_scope_as = sender }
	}

	# Oops
	option = {
		name = cid.5000.a
		ai_chance = {
			base = 1000 # Historically determined
		}
	}
	
	after = {
		random_owned_story = {
			limit = {
				story_type = story_el_cid
			}
			end_story = yes
		}
	}
}
